// Test_LDAP_Client.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "stdafx.h"
#include "Administrator.h"
#include <process.h>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{

	try
	{

		vector<Atribut<char*>> atribute;

		printf("                         **LDAP Client API***\n");
		printf("____________________________________________________________________________\n");
		printf("____________________________________________________________________________\n");

		
		char *DomainName[10] = {"tup","com"};
		int DomainName_Level = 2;
		char *opts[10]={"-LLL"};
		printf("\n*Nodul radacina al arborelui ce formeaza directorul LDAP este:");
		for(int i=0;i<DomainName_Level;i++)
			printf(" dc=%s ",DomainName[i]);
		printf("*");
		
		

		//create the LDAP Server Admin
		printf("\n\n            ->->Se creaza administratorul seserverului LDAP...\n");

		
		Administrator *admin = new Administrator("admin",DomainName,2);

		//connect & bind to LDAP Server
		printf("\n->->Crearea unei conexiuni intre administrator si server...");
		admin->connectToServer("192.168.136.131",27015);

		admin->Bind("admin","LDAP administrator");//admin_name & admin_password

		printf("\n\n--------------------------------------------------------------------------\n");
		printf("Operatiile de adaugare,stergere si cautare a unor utilizatori ai serverului LDAP\n");
		printf("------------------------------------------------------------------------------\n");

		admin->createAccount("user1","user1pass","user1@mail");
		admin->createAccount("user2","user2pass","user2@mail");
		admin->createAccount("user3","user3pass","user3@mail");
		
		//delete an account
		admin->deleteAccount("user3");

		//search for an account
		admin->searchAccount("user1");
	
		printf("\n\n__________________________________________________________________________\n");

		//create the simple LDAP client...
		printf("\n               ->->Se creaza un client simplu LDAP...\n\n");
		printf("\n____________________________________________________________________________\n");
	
		Client * client = new Client("user1","people",DomainName,2);

		Atribut<char *>a10("sn","user1pass"),a11("givenName","user1");
		atribute.clear();
		atribute.push_back(a10);
		atribute.push_back(a11);


		printf("\n->Se creaza o conexiune cu serverul...\n\n");

		client->connectToServer("192.168.136.131",27015);


		printf("\n->Procesul de autentificare la server...\n\n");
		client->Bind("user1","user1pass");//user_name+parola
	
		printf("\n\n--------------------------------------------------------------------------\n");
		printf("\n-              >Se executa operatia compareAtribute...\n\n");
		//client->compareAtribute("dc=tup,dc=com",SIMPLE_AUTH,opts,1,"ldap:///",atribute);

		
		Atribut<char *> a3("objectClass",""),a4("sn","");
		atribute.clear();
		atribute.push_back(a3);
		atribute.push_back(a4);

		printf("\n\n--------------------------------------------------------------------------\n");
		printf("\n->Cele doua operatii de cautare...\n\n");
	
		client->Search(SIMPLE_AUTH,opts,1,"ldap:///",true,atribute);

		client->Search(SIMPLE_AUTH,opts,1,"ldap:///",true);


		
		//create the full LDAP client...
		printf("\n____________________________________________________________________________\n");
		printf("             ->->Se creaza un  client privilegiat LDAP...");
		printf("\n____________________________________________________________________________\n");
	
		

		char *opt[10]={"-D"};

		ClientFull *cf=new ClientFull("user2","people",DomainName,2);
	

		
		printf("\n->Se creaza o conexiune cu serverul...\n\n");

		cf->connectToServer("192.168.136.131",27015);
	
		printf("\n\n--------------------------------------------------------------------------");
		printf("\n           ->Procesul de autentificare la server...\n\n");

		cf->Bind("user2","user2pass");//user_name+parola

		Atribut<char *> a5("mail","user2@mail");
		atribute.clear();
		atribute.push_back(a5);

		printf("\n\n--------------------------------------------------------------------------");
		printf("\n            ->Operatia de adaugare a unor atribute...\n\n");
		cf->AddAtributes("cn=admin,dc=tup,dc=com",0,"secret",opt,1,atribute);


		printf("\n\n--------------------------------------------------------------------------");
		printf("\n           ->Operatia de stergere a unor atribute din intrarea clientului...\n\n");
		cf->DeleteAtributeFromEntry("cn=admin,dc=tup,dc=com",SIMPLE_AUTH,"secret",opt,1,atribute);

		atribute.clear();
		a5.setAtribut("cn","client2");
		atribute.push_back(a5);

		printf("\n\n--------------------------------------------------------------------------");
		printf("\n            ->Operatia de modificare a unor atribute din intrarea clientului...\n\n");
		cf->ModifyAtributes("cn=admin,dc=tup,dc=com",SIMPLE_AUTH,"secret",opt,1,atribute);

		printf("\n\n--------------------------------------------------------------------------");
		printf("\n           ->Operatia de adaugare a unui atribut tip JPG...\n\n");
		cf->AddJPGPhoto_Atribute("cn=admin,dc=tup,dc=com",0,"secret",opt,1,"image.jpg");

		cout<<"\n\n--------------------\n\n";
		system("pause");
		system("pause");
		
	}
	catch(ClientNetwork::ConexiuneEronata *ce)
	{
		ce->printEroare();
		system("pause");
		delete ce;
	}
	catch(exception *e)
	{
		system("pause");
		cout<<"Eroare la alocarea dinamica.\n";
		delete e;
	}
	
}

